package com.zx.qrcode;

import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;

import me.dm7.barcodescanner.zbar.ZBarScannerView;

//public class MainActivity extends AppCompatActivity implements ZXingScannerView.ResultHandler{
public class MainActivity extends AppCompatActivity implements ZBarScannerView.ResultHandler{
    private static final String TAG = "zx";
//    private ZXingScannerView mScannerView;
    private ZBarScannerView mScannerView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
//        mScannerView = (ZXingScannerView) findViewById(R.id.squareZXingScannerView);
//        mScannerView.startCamera(Camera.CameraInfo.CAMERA_FACING_BACK);

        mScannerView = (ZBarScannerView) findViewById(R.id.scannerView);
    }

    public void onResume() {
        super.onResume();
        mScannerView.setResultHandler(this); // Register ourselves as a handler for scan results.
        mScannerView.startCamera();          // Start camera on resume
    }

    @Override
    public void onPause() {
        super.onPause();
        mScannerView.stopCamera();           // Stop camera on pause
    }

    @Override
    public void handleResult(me.dm7.barcodescanner.zbar.Result rawResult) {
        // Do something with the result here
        Toast.makeText(this, "Contents = " + rawResult.getContents()+
                ", Format = " + rawResult.getBarcodeFormat().toString(), Toast.LENGTH_SHORT).show();

        //2秒钟之后继续扫描
        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                mScannerView.resumeCameraPreview(MainActivity.this);
            }
        }, 2000);
    }


//    @Override
//    public void handleResult(Result rawResult) {
//        // Do something with the result here
//        Toast.makeText(this, "Contents = " + rawResult.getText() +
//                ", Format = " + rawResult.getBarcodeFormat().toString(), Toast.LENGTH_SHORT).show();
//
//        //2秒钟之后继续扫描
//        Handler handler = new Handler();
//        handler.postDelayed(new Runnable() {
//            @Override
//            public void run() {
//                mScannerView.resumeCameraPreview(MainActivity.this);
//            }
//        }, 2000);
//    }
}
